#include "DeltaGlider.h"
// --------------------------------------------------------------
// Process direct key events
// --------------------------------------------------------------
int DeltaGlider::clbkConsumeDirectKey (char *kstate)
{
	if (KEYMOD_ALT (kstate)) {
		if (KEYDOWN (kstate, OAPI_KEY_ADD)) { // increment scram thrust
			//if (scramjet)
				for (int i = 0; i < 2; i++) {
					IncThrusterLevel (th_main[i], oapiGetSimStep() * 0.3);
					scram_intensity[i] = GetThrusterLevel (th_main[i]) * scram_max[i];
				}
			RESETKEY (kstate, OAPI_KEY_ADD);
		}
		if (KEYDOWN (kstate, OAPI_KEY_SUBTRACT)) { // decrement scram thrust
			//if (scramjet)
				for (int i = 0; i < 2; i++) {
					IncThrusterLevel (th_main[i], oapiGetSimStep() * -0.3);
					scram_intensity[i] = GetThrusterLevel (th_main[i]) * scram_max[i];
				}
			RESETKEY (kstate, OAPI_KEY_SUBTRACT);
		}
	}
	return 0;
}

// --------------------------------------------------------------
// Process buffered key events
// --------------------------------------------------------------
int DeltaGlider::clbkConsumeBufferedKey (DWORD key, bool down, char *kstate)
{

	
	
	if (!down) return 0; // only process keydown events
	if (Playback()) return 0; // don't allow manual user input during a playback

	if (KEYMOD_SHIFT (kstate)) {
	} else if (KEYMOD_CONTROL (kstate)) {
		switch (key) {
		case OAPI_KEY_SPACE: // open control dialog
			//oapiOpenDialogEx (g_Param.hDLL, IDD_CTRL, Ctrl_DlgProc, DLG_CAPTIONCLOSE, this);
			return 1;
		case OAPI_KEY_B:
			RevertAirbrake ();
			return 1;
		}
	} else {
		switch (key) {
		case OAPI_KEY_D:  // "operate radiator"
			RevertRadiator ();
			return 1;
		case OAPI_KEY_G:  // "operate landing gear"
			RevertLandingGear ();
			return 1;
		case OAPI_KEY_O:  // "operate outer airlock"
			RevertOuterAirlock ();
			return 1;
		case OAPI_KEY_V:  // "operate outer airlock"
			RevertReverser ();
			return 1;
		
		}

	}
	
	//return 0;
	
	
	
	
		// only process keydown events
	if (!down) 
		return 0; 

	//---------------------------------------------------------------------------
	// Ummu Key "E" perform the EVA of the selected member
	// TIPS: use standard miscId function and mesh name to be 
	// compatible with other ship (see UmmuSDK.h constant "UmmuMiscID","UmmuFunctionName" 
	// and "UmmuMeshUsed" and function SetAlternateMeshToUseForEVASpacesuit()
	// just before EVA)
	//
	// GOOD IDEA: use standard mesh with different spacesuit in function of MiscID as do the DGIV.
	//            try it: just replace "Crew.EvaCrewMember" below by "EvaCrewMemberWithStandardSpacesuit"
	//            and try the "demo eva" scenario. This function is a helper that you can find 
	//            at bottom of this listing with complete explaination.
	//
	// ADD REALISM: It's your responsabilities also to set ship's control accordingly to crew aboard.
	//				If you want to disable control if no one is aboard have a look at "SetADCtrlMode()"
	//				and "SetAttitudeMode()" functions of Orbiter.
	if(key==OAPI_KEY_E)
	{
		// PERFORM THE EVA, first we get is name with "GetCrewNameBySlotNumber" then we perform EVA with "EvaCrewMember"
		int Returned=Crew.EvaCrewMember(Crew.GetCrewNameBySlotNumber(SelectedUmmuMember));
		// we provide feedback to user (You can display a message on panel or wathewer)
		// here below all the return code possible:
		switch(Returned)
		{
		case TRANSFER_TO_DOCKED_SHIP_OK:
			sprintf(SendFocusScreenMessage(),"Transfer to docked ship Ok - %s transfered",Crew.GetLastEvaedCrewName());SelectedUmmuMember=0;
			break;
		case EVA_OK:
			sprintf(SendFocusScreenMessage(),"EVA OK - %s left the ship",Crew.GetLastEvaedCrewName());SelectedUmmuMember=0;
			break;
		case ERROR_NO_ONE_ON_BOARD:
			strcpy(SendFocusScreenMessage(),"Error, no one on board, unable to EVA");
			break;
		case ERROR_AIRLOCK_CLOSED:
			strcpy(SendFocusScreenMessage(),"Error, airlock is closed, unable to EVA");
			break;
		case ERROR_DOCKED_SHIP_HAVE_AIRLOCK_CLOSED:
			strcpy(SendFocusScreenMessage(),"Error, docked ship's airlock is closed, unable to transfer");
			break;
		case ERROR_DOCKED_SHIP_IS_FULL:
			strcpy(SendFocusScreenMessage(),"Error, docked ship is already full transfer failed");
			break;
		case ERROR_CREW_MEMBER_NOT_FOUND:
			strcpy(SendFocusScreenMessage(),"Error, no crew by this name in ship");
			break;
		case ERROR_DOCKEDSHIP_DONOT_USE_UMMU:
			strcpy(SendFocusScreenMessage(),"Error, docked ship do not use UMmu 1.5, ask author to add it See http://orbiter.dansteph.com");
			break;
		case ERROR_MISC_ERROR_EVAFAILED:
			strcpy(SendFocusScreenMessage(),"Misc error with UMMU See http://orbiter.dansteph.com to install it again");
			break;
		}
		return TRUE;
	}

	//---------------------------------------------------------------------------
	// Ummu Key "1" Select next member This is just internal to the demo
	// you may do your own selection system by panel button, name etc etc
	if(key==OAPI_KEY_1)
	{
		// we test there is someone aboard
		if(Crew.GetCrewTotalNumber()==0)
		{
			strcpy(SendFocusScreenMessage(),"Sorry no one aboard unable to select");	
			return 1;
		}

		// we test that we select existing member
		//if(SelectedUmmuMember<Crew.GetCrewTotalNumber()-1)
		//	SelectedUmmuMember++;
		char * Name=Crew.GetCrewNameBySlotNumber(SelectedUmmuMember);
		sprintf(SendFocusScreenMessage(),"Slot %i  %s \"%s\" aged %i Selected for EVA or Transfer, please press \"E\" to EVA \"ESC\" to eject",SelectedUmmuMember,Crew.GetCrewMiscIdBySlotNumber(SelectedUmmuMember),Name,Crew.GetCrewAgeBySlotNumber(SelectedUmmuMember));
		return 1;
	}

	//---------------------------------------------------------------------------
	// Ummu Key "2" Select previous member This is just internal to the demo
	// you may do your own selection system by panel button
	if(key==OAPI_KEY_2)
	{
		// we test there is someone aboard
		if(Crew.GetCrewTotalNumber()==0)
		{
			strcpy(SendFocusScreenMessage(),"Sorry no one aboard unable to select");	
			return 1;
		}
		if(SelectedUmmuMember>0)
			SelectedUmmuMember--;
		char * Name=Crew.GetCrewNameBySlotNumber(SelectedUmmuMember);
		sprintf(SendFocusScreenMessage(),"Slot %i %s \"%s\" aged %i Selected for EVA or Transfer, please press \"E\" to EVA \"ESC\" to eject",SelectedUmmuMember,Crew.GetCrewMiscIdBySlotNumber(SelectedUmmuMember),Name,Crew.GetCrewAgeBySlotNumber(SelectedUmmuMember));
		return 1;
	}


	//---------------------------------------------------------------------------
	// Ummu Key "A" Switch the airlock door on/off
	if(key==OAPI_KEY_A)
	{
		// switch state
		Crew.SetAirlockDoorState(!Crew.GetAirlockDoorState());
		// display state
		if(Crew.GetAirlockDoorState()==TRUE)
			strcpy(SendFocusScreenMessage(),"Airlock is now open");	
		else
			strcpy(SendFocusScreenMessage(),"Airlock is now closed");	
		return 1;
	}

	//---------------------------------------------------------------------------
	// ADD some Fun, Eject the guy, No check of all return code here to keep listing small and clear.
	// Notice eject function doesn't check airlock state at all.
	// GOOD IDEA: Get the Object's handle after ejection with function "GetObjHandleOfLastEVACrew"
	// and add to it one very small tank, thruster and smoke, then fire thruster (see pilot ejection of DGIV)
	// BAD IDEA: Not testing the handle returned by "GetObjHandleOfLastEVACrew" before using may
	// cause a CTD if by any bad luck the pointer is invalid (handle==NULL)
	if(key==OAPI_KEY_ESCAPE)
	{
		if(Crew.EjectCrewMember(Crew.GetCrewNameBySlotNumber(SelectedUmmuMember))==EVA_OK)
			sprintf(SendFocusScreenMessage(),"%s EJECTED",Crew.GetLastEvaedCrewName());SelectedUmmuMember=0;
		return 1;
	}

	//---------------------------------------------------------------------------
	// Use a different Mesh (type "C" then EVA someone)
	if(key==OAPI_KEY_C)
	{
		Crew.SetAlternateMeshToUseForEVASpacesuit("mmu");	// the stock mmu of orbiter located in "meshes/mmu.msh"
		strcpy(SendFocusScreenMessage(),"Mesh changed");
		return 1;
	}

	//---------------------------------------------------------------------------
	// Get some infos Name of ship and total soul aboard
	if(key==OAPI_KEY_S)
	{
		sprintf(SendFocusScreenMessage(),"%i souls aboard ship %s, %i seats available",Crew.GetCrewTotalNumber(),GetName(),4-Crew.GetCrewTotalNumber());
		return 1;
	}
	return 0;
	
	
	
}